﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Evan.Input
{
    public class InputHelper
    {
        #region 判断对象是否为空  
        /// <summary>  
        /// 判断对象是否为空，为空返回true  
        /// </summary>  
        /// <typeparam name="T">要验证的对象的类型</typeparam>  
        /// <param name="data">要验证的对象</param>          
        public static bool IsNullOrEmpty<T>(T data)
        {
            //如果为null  
            if (data == null)
            {
                return true;
            }

            //如果为""  
            if (data.GetType() == typeof(String))
            {
                if (string.IsNullOrEmpty(data.ToString().Trim()))
                {
                    return true;
                }
            }

            //如果为DBNull  
            if (data.GetType() == typeof(DBNull))
            {
                return true;
            }

            //不为空  
            return false;
        }

        /// <summary>  
        /// 判断对象是否为空，为空返回true  
        /// </summary>  
        /// <param name="data">要验证的对象</param>  
        public static bool IsNullOrEmpty(object data)
        {
            //如果为null  
            if (data == null)
            {
                return true;
            }

            //如果为""  
            if (data.GetType() == typeof(String))
            {
                if (string.IsNullOrEmpty(data.ToString().Trim()))
                {
                    return true;
                }
            }

            //如果为DBNull  
            if (data.GetType() == typeof(DBNull))
            {
                return true;
            }

            //不为空  
            return false;
        }
        #endregion

        #region  验证EMail是否合法  
        /// <summary>  
        /// 验证EMail是否合法  
        /// </summary>  
        /// <param name="email">要验证的Email</param>  
        public static bool IsEmail(string email)
        {
            //如果为空，认为验证不合格  
            if (IsNullOrEmpty(email))
            {
                return false;
            }

            //清除要验证字符串中的空格  
            email = email.Trim();

            //模式字符串  
            string pattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";

            //验证  
            return RegexHelper.IsMatch(email, pattern);
        }
        #endregion

        #region 验证手机是否合法
        public static bool IsMobilePhone(string mobile)
        {
            //如果为空，认为验证不合格  
            if (IsNullOrEmpty(mobile))
            {
                return false;
            }

            mobile = mobile.Trim();

            string pattern = @"^[1][3,4,5,6,7,8,9][0-9]{9}$";

            return RegexHelper.IsMatch(mobile, pattern);
        }
        #endregion

        #region 验证身份证是否合法  
        /// <summary>  
        /// 验证身份证是否合法  
        /// </summary>  
        /// <param name="idCard">要验证的身份证</param>          
        public static bool IsIdCard(string idCard)
        {
            //如果为空，认为验证合格  
            if (IsNullOrEmpty(idCard))
            {
                return false;
            }

            //清除要验证字符串中的空格  
            idCard = idCard.Trim();

            //模式字符串  
            StringBuilder pattern = new StringBuilder();
            pattern.Append(@"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|");
            pattern.Append(@"50|51|52|53|54|61|62|63|64|65|71|81|82|91)");
            pattern.Append(@"(\d{13}|\d{15}[\dx])$");

            //验证  
            return RegexHelper.IsMatch(idCard, pattern.ToString());
        }
        #endregion

        #region 验证日期格式是否正确
        /// <summary>
        /// 验证日期格式是否正确
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsDate(string date)
        {
            //如果为空，认为验证合格  
            if (IsNullOrEmpty(date))
            {
                return false;
            }

            //清除要验证字符串中的空格  
            date = date.Trim();

            //模式字符串  
            string pattern = @"^(\d{4})(-)(\d{2})(-)(\d{2})$";

            //验证  
            return RegexHelper.IsMatch(date, pattern);
        }
        public static bool IsTime(string time)
        {
            //如果为空，认为验证合格  
            if (IsNullOrEmpty(time))
            {
                return false;
            }

            //清除要验证字符串中的空格  
            time = time.Trim();

            //模式字符串  
            string pattern = @"^(\d{4})(-)(\d{2})(-)(\d{2}) (\d{2}):(\d{2}):(\d{2}).(\d{3})$";

            //验证  
            return RegexHelper.IsMatch(time, pattern);
        }
        #endregion

        #region 检测客户的输入中是否有危险字符串  
        /// <summary>  
        /// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。  
        /// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。  
        /// </summary>  
        /// <param name="input">要检测的字符串</param>  
        public static bool IsValidInput(ref string input)
        {
            try
            {
                if (IsNullOrEmpty(input))
                {
                    //如果是空值,则跳出  
                    return true;
                }
                else
                {
                    //替换单引号  
                    input = input.Replace("'", "''").Trim();

                    //检测攻击性危险字符串  
                    string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
                    string[] testArray = testString.Split('|');
                    foreach (string testStr in testArray)
                    {
                        if (input.ToLower().IndexOf(testStr) != -1)
                        {
                            //检测到攻击字符串,清空传入的值  
                            input = "";
                            return false;
                        }
                    }

                    //未检测到攻击字符串  
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 验证是否Base64
        private static char[] base64CodeArray = new char[]
        {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4',  '5', '6', '7', '8', '9', '+', '/', '='
        };
        /// <summary>
        /// 是否base64字符串
        /// </summary>
        /// <param name="base64Str">要判断的字符串</param>
        /// <returns></returns>
        public static bool IsBase64(string base64Str)
        {
            byte[] bytes = null;
            return IsBase64(base64Str, out bytes);
        }
        /// <summary>
        /// 是否base64字符串
        /// </summary>
        /// <param name="base64Str">要判断的字符串</param>
        /// <param name="bytes">字符串转换成的字节数组</param>
        /// <returns></returns>
        public static bool IsBase64(string base64Str, out byte[] bytes)
        {
            //string strRegex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
            bytes = null;
            if (string.IsNullOrEmpty(base64Str))
                return false;
            else
            {
                if (base64Str.Contains(","))
                    base64Str = base64Str.Split(',')[1];
                if (base64Str.Length % 4 != 0)
                    return false;
                if (base64Str.Any(c => !base64CodeArray.Contains(c)))
                    return false;
            }
            try
            {
                bytes = Convert.FromBase64String(base64Str);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }


        }
        #endregion
    }
}
